package com.jeff.android.lab.module.widget;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jeff.android.lab.R;
import com.jeff.android.lab.module.widget.constraint.ConstraintLayoutFragment;
import com.jeff.android.lab.module.widget.custom.CustomWidgetFragment;
import com.jeff.android.lab.module.widget.fragment.WidgetFragmentActivity;
import com.jeff.android.lab.module.widget.webview.WebViewActivity;
import com.jeff.android.lab.module.widget.webview.WebViewFragment;
import com.jeff.android.lab.module.widget.webview.WebViewPool;
import com.jeff.android.lab.module.widget.webview.WebViewPoolActivity;
import com.jeff.android.lab.module.widget.webview.WebViewPoolFragment;
import com.jeff.android.lab.util.RouterUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;

/**
 * Android 系统自带和扩展库中的组件研究
 *
 * @author: duff
 * @date: 2019/1/10
 * @since: 1.0.0
 */
public class WidgetMainFragment extends Fragment {
    @BindView(R.id.ptr)
    PtrClassicFrameLayout mPtr;
    @BindView(R.id.rlv)
    RecyclerView mRecyclerView;

    private List<String> mWidgets = new ArrayList<String>() {
        {
            add("custom");
            add("Fragment");
            add("WebView");
            add("WebView-pool");
            add("BottomSheetBehavior");
            add("BottomSheetDialog");
            add("constraint"); // 约束布局
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.common_ptr_recycler_view,
                container, false);
        ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        WebViewPool.getInstance().init();
    }

    private void initData() {
        mPtr.setEnabled(false);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mWidgetAdapter);
        mWidgetAdapter.setNewData(mWidgets);
    }

    private void launch(String widget) {
        if ("custom".equals(widget)) {
            RouterUtil.launchFragmentActivity(getContext(), CustomWidgetFragment.class);
        } else if ("Fragment".equals(widget)) {
            RouterUtil.launchActivity(getContext(), WidgetFragmentActivity.class);
        } else if ("WebView".equals(widget)) {
            RouterUtil.launchActivity(getContext(), WebViewActivity.class);
        } else if ("WebView-pool".equals(widget)) {
            RouterUtil.launchActivity(getContext(), WebViewPoolActivity.class);
        } else if ("BottomSheetBehavior".equals(widget)) {
            RouterUtil.launchFragmentActivity(getContext(), BottomSheetBehaviorFragment.class);
        } else if ("BottomSheetDialog".equals(widget)) {
            RouterUtil.launchFragmentActivity(getContext(), BottomSheetDialogFragment.class);
        } else if ("constraint".equals(widget)) {
            RouterUtil.launchFragmentActivity(getContext(), ConstraintLayoutFragment.class);
        }
    }

    BaseQuickAdapter mWidgetAdapter =
            new BaseQuickAdapter<String, BaseViewHolder>(R.layout.common_simple_item_text) {
                @Override
                protected void convert(BaseViewHolder helper, final String item) {
                    TextView text = helper.getView(R.id.tv_simple_item);
                    text.setText(item);

                    helper.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            launch(item);
                        }
                    });
                }
            };

}
